package com.smartao.base.role;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartao.core.dao.BaseEntityDao;

@Repository
public class RoleDaoImpl extends BaseEntityDao<Role, Long> implements RoleDao {

	@Override
	public Role findByRoleKey(String roleKey) {
       return findUnique(Restrictions.eq("roleKey", roleKey));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> findByRoleType(Long roleTypeId) {
		String hql = "FROM Role where roleType.id=:roleTypeId";
		return getSession().createQuery(hql)
				.setLong("roleTypeId", roleTypeId)
				.list();
	}
	

	@Override
	public Role findRoleAndUser(Long roleId){
		Role role = (Role) getSession().createCriteria(Role.class)
				.add(Restrictions.eq("id", roleId))
				.setFetchMode("users", FetchMode.JOIN)
				.uniqueResult();
		return role;
	}

	@Override
	public void addUserToRole(Long roleId, Long[] employeeIds) {
	}

}
